part of '../repository.dart';

const _kCustomerCancellationPurchasedSubscriptionExpirationReasonValue = 'customer_cancellation';
const _kBillingErrorPurchasedSubscriptionExpirationReasonValue = 'billing_error';
const _kPriceIncreasePurchasedSubscriptionExpirationReasonValue = 'price_increase';
const _kUnavailablePurchasePurchasedSubscriptionExpirationReasonValue = 'unavailable_purchase';

class _PurchasedSubscriptionExpirationReasonDto extends Equatable {
  const _PurchasedSubscriptionExpirationReasonDto._internal({
    required this.expirationReasonString,
  });

  final String expirationReasonString;

  @override
  List<Object?> get props {
    return [
      expirationReasonString,
    ];
  }

  factory _PurchasedSubscriptionExpirationReasonDto.fromString(String expirationReasonString) {
    return _PurchasedSubscriptionExpirationReasonDto._internal(
      expirationReasonString: expirationReasonString,
    );
  }

  factory _PurchasedSubscriptionExpirationReasonDto.fromEntity(
      AppStorePurchasedSubscriptionExpirationReason expirationReason) {
    late final String expirationReasonString;

    if (expirationReason == AppStorePurchasedSubscriptionExpirationReason.customerCancellation) {
      expirationReasonString = _kCustomerCancellationPurchasedSubscriptionExpirationReasonValue;
    } else if (expirationReason == AppStorePurchasedSubscriptionExpirationReason.billingError) {
      expirationReasonString = _kBillingErrorPurchasedSubscriptionExpirationReasonValue;
    } else if (expirationReason == AppStorePurchasedSubscriptionExpirationReason.priceIncrease) {
      expirationReasonString = _kPriceIncreasePurchasedSubscriptionExpirationReasonValue;
    } else if (expirationReason ==
        AppStorePurchasedSubscriptionExpirationReason.unavailablePurchase) {
      expirationReasonString = _kUnavailablePurchasePurchasedSubscriptionExpirationReasonValue;
    }

    return _PurchasedSubscriptionExpirationReasonDto._internal(
      expirationReasonString: expirationReasonString,
    );
  }

  @override
  String toString() {
    return expirationReasonString;
  }

  AppStorePurchasedSubscriptionExpirationReason toEntity() {
    if (expirationReasonString ==
        _kCustomerCancellationPurchasedSubscriptionExpirationReasonValue) {
      return AppStorePurchasedSubscriptionExpirationReason.customerCancellation;
    }

    if (expirationReasonString == _kBillingErrorPurchasedSubscriptionExpirationReasonValue) {
      return AppStorePurchasedSubscriptionExpirationReason.billingError;
    }

    if (expirationReasonString == _kPriceIncreasePurchasedSubscriptionExpirationReasonValue) {
      return AppStorePurchasedSubscriptionExpirationReason.priceIncrease;
    }

    if (expirationReasonString == _kUnavailablePurchasePurchasedSubscriptionExpirationReasonValue) {
      return AppStorePurchasedSubscriptionExpirationReason.unavailablePurchase;
    }

    throw UnimplementedError();
  }
}
